package cn.iocoder.yudao.module.promotion.service.prizedraw;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawCouponPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawSettingRespVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.prizedraw.PrizeDrawSettingDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.prizedraw.PrizeDrawSettingMapper;
import cn.iocoder.yudao.module.promotion.enums.PrizeDrawSettingTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

@Service
@Validated
public class PrizeDrawSettingServiceImpl extends ServiceImpl<PrizeDrawSettingMapper, PrizeDrawSettingDO> implements PrizeDrawSettingService {


    /**
     * 获取抽奖设置信息
     *
     * @return 抽奖设置信息
     */
    @Override
    public PrizeDrawSettingRespVO getPrizeDrawSettingInfo() {
        PrizeDrawSettingDO mpds;

        List<PrizeDrawSettingDO> list = this.list();
        if (CollectionUtil.isNotEmpty(list)) {
            mpds = CollUtil.get(list, 0);
        } else {
            mpds = new PrizeDrawSettingDO();
            mpds.setPrizeDrawSettingId(1L);
            mpds.setSettingType("");
            save(mpds);
        }

        return BeanUtil.copyProperties(mpds, PrizeDrawSettingRespVO.class);
    }

    /**
     * 保存
     *
     * @param dto 抽奖设置信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void preserve(PrizeDrawSettingDO dto) {
        if (StrUtil.isBlankOrUndefined(dto.getSettingType())) {
            throw exception(new ErrorCode(500, "抽奖设置类型至少要选择一种"));
        }

        if (!StrUtil.contains(dto.getSettingType(), PrizeDrawSettingTypeEnum.DRAGON_BEANS.getCode())) {
            dto.setDragonBeans(null);
        }

        if (!StrUtil.contains(dto.getSettingType(), PrizeDrawSettingTypeEnum.INVITING.getCode())) {
            dto.setInviteNums(null);
        }

        if (!StrUtil.contains(dto.getSettingType(), PrizeDrawSettingTypeEnum.SIGN.getCode())) {
            dto.setSigninNums(null);
        }

        this.saveOrUpdate(dto);
    }

    /**
     * 优惠券类型列表分页查询
     *
     * @param dto 查询条件
     * @return
     */
    @Override
    public IPage<PrizeDrawSettingRespVO> couponPageListAdmin(PrizeDrawCouponPageReqVO dto) {
        //TODO
        return null;
    }

    /**
     * 龙豆总额
     *
     * @return 龙豆总额
     */
    @Override
    public Long dragonTotal() {
        //TODO
        return null;
    }
}